﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

using Utils;

public partial class order_check : FBasePage
{
    // Fields
    public NameValueCollection _system = new NameValueCollection();

    public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
    public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();
    public bool falg = false;
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public string orderid = "A007";
    public List<Maticsoft.Model.Order> orderlist = new List<Maticsoft.Model.Order>();
    public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
    public string pagenum = "1";
    public string service = "";
    public string strpage;
    public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
    public SystemConfig sysconfig = new SystemConfig();
    public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
    public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
    public Maticsoft.Model.Team teamodel = new Maticsoft.Model.Team();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();

    // Methods
    public void ConfirmOrder()
    {
        Maticsoft.Model.Order order = new Maticsoft.Model.Order();
        Maticsoft.Model.UserInfo model = new Maticsoft.Model.UserInfo();
        model = this.userbll.GetModel(this.userbll.Getuserid(base.UserName));
        order = this.orderbll.GetModel(Convert.ToInt32(this.hiorderid.Value));
        if (order != null)
        {
            if (model.Money < order.Origin)
            {
                if (base.Request["pd_FrpId"] != null)
                {
                    this.service = "yeepay";
                }
                else if (base.Request["paytype"] != null)
                {
                    this.service = base.Request["paytype"].ToString();
                }
                else
                {
                    this.service = "";
                }
                if (this.service == "")
                {
                    this.SetError("请您选择支付方式");
                }
                else
                {
                    this.orderbll.updatebyservice(this.hiorderid.Value, this.service, this.orderbll.Getorder(order.User_id, order.Team_id, Convert.ToInt32(this.hiorderid.Value)), order.Origin);
                    base.Response.Redirect(string.Concat(new object[] { "pay.aspx?orderid=", order.Id, "&pd_FrpId=", base.Request["pd_FrpId"] }));
                }
            }
            else
            {
                this.service = "credit";
                this.orderbll.updatebyservice(this.hiorderid.Value, this.service, this.orderbll.Getorder(order.User_id, order.Team_id, Convert.ToInt32(this.hiorderid.Value)), order.Origin);
                base.Response.Redirect("pay.aspx?orderid=" + this.hiorderid.Value + "&pd_FrpId=" + base.Request["pd_FrpId"]);
            }
        }
        else
        {
            this.SetError("订单不存在");
        }
    }

    public void GetOrder()
    {
        Maticsoft.Model.UserInfo info = new Maticsoft.Model.UserInfo();
        this.ordermodel = this.orderbll.GetModel(Convert.ToInt32(this.hiorderid.Value));
        this.blockcard1.UpdateView(Convert.ToInt32(this.hiorderid.Value));
    }

    public void GetSystem()
    {
        this.sysmodel = this.sysbll.GetModel(1);
    }

    public string GetTeam(string id, int orderid)
    {
        string str = "";
        this.teamodel = this.teambll.GetModel(Convert.ToInt32(id));
        if (this.teamodel != null)
        {
            return string.Concat(new object[] { "<a  href='../team.aspx?id=", this.teamodel.Id, "' target=_blank>", this.teamodel.Title, "</a>" });
        }
        this.detaillist = this.detailbll.Searchorderdetail(orderid);
        if (this.detaillist != null)
        {
            int num = 0;
            foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
            {
                num++;
                object obj2 = str;
                str = string.Concat(new object[] { obj2, "<a href='/team.aspx?id=", orderdetail.Teamid, "' target=_blank>", num, ":" });
                str = str + ((this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)) == null) ? "" : this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)).Title);
                str = str + "</a><br>";
            }
            return str;
        }
        return "";
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this._system = this.sysconfig.GetSystem();
        if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
        {
            base.Response.Redirect("/account/Login.aspx");
        }
        if (base.Request["orderid"] != null)
        {
            if (Utility.IsNum(base.Request["orderid"].ToString()))
            {
                this.hiorderid.Value = base.Request["orderid"].ToString();
                this.ordermodel = this.orderbll.GetModel(Convert.ToInt32(this.hiorderid.Value));
                if (this.ordermodel != null)
                {
                    if (((this.ordermodel.Team_id == 0) || (this.ordermodel.Team_id.ToString() == "")) || (this.ordermodel.Team_id.ToString() == null))
                    {
                        base.Response.Redirect("../shopcart/confirmation.aspx?orderid=" + this.ordermodel.Id);
                    }
                    Maticsoft.Model.Team model = new Maticsoft.BLL.Team().GetModel(this.ordermodel.Team_id);
                    if (model != null)
                    {
                        TeamState state = base.GetState(model);
                        if ((state != TeamState.begin) && (state != TeamState.successbuy))
                        {
                            this.SetError("该项目已过期或已卖光，不能支付！");
                            base.Response.Redirect("/index.aspx");
                            base.Response.End();
                            return;
                        }
                    }
                    this.GetOrder();
                    goto Label_0231;
                }
                this.SetError("订单不存在");
            }
            else
            {
                this.SetError("您输入的参数非法");
            }
            return;
        }
        this.SetError("参数不正确");
        base.Response.Redirect("~/index.aspx");
    Label_0231:
        this.GetSystem();
        if (base.Request.Form["btnadd"] == "确认订单，付款")
        {
            if (!(base.IsLogin || !(base.UserName == "")))
            {
                base.Response.Redirect("/account/Login.aspx");
            }
            this.ConfirmOrder();
        }
    }

}